package com.jvyou.mybatis.builder.parser;


import com.jvyou.mybatis.builder.enums.ConfigItemName;
import com.jvyou.mybatis.exception.JvyouMybatisException;
import com.jvyou.mybatis.utils.string.StringUtils;
import org.dom4j.Element;

import java.util.List;
import java.util.Properties;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/7/20 11:20
 * @Description XML 配置节点处理器接口
 */
public interface XmlConfigNodeHandler extends ConfigItemName {

    void handle(Element element, XmlConfigContext context);

    @SuppressWarnings("unchecked")
    default Properties getProperties(Element element) {
        Properties properties = new Properties();
        List<Element> elements = element.elements(PROPERTY);
        for (Element e : elements) {
            String name = e.attributeValue(NAME);
            if (name == null || name.isEmpty()) {
                throw new JvyouMybatisException("dataSource 节点的 property 节点的 name 属性不能为空");
            }
            String value = e.attributeValue(VALUE);
            if (StringUtils.isEmpty(value)) {
                throw new JvyouMybatisException("dataSource 节点的 property 节点 " + name + " 配置的 value 属性不能为空");
            }
            properties.setProperty(name, value);
        }
        return properties;
    }

}
